package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.dto.ReadBehaviorDto;
import com.heima.behavior.dto.UpdateArticleMessage;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApReadBehavior;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApReadBehaviorService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP阅读行为表 服务实现类
 * </p>
 *
 * @author wsj
 * @since 2022-03-17
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements IApReadBehaviorService {
    @Autowired
    private IApBehaviorEntryService entryService;
    @Value("${topic.hotArticleScoreTopic}")
    private String hotArticleScoreTopic;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        //保存阅读行为
        //获取线程中的user
        User user = AppThreadLocalUtil.get();
        if (user==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //保存实体
        EntryDto entryDto = new EntryDto();
        entryDto.setEquipmentId(dto.getEquipmentId());
        entryDto.setUserId(user.getId());
        ApBehaviorEntry entry = entryService.saveEntry(entryDto);
        //保存APP阅读行为表
        //构建条件查询是否存在
        LambdaQueryWrapper<ApReadBehavior> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApReadBehavior::getArticleId, dto.getArticleId());
        wrapper.eq(ApReadBehavior::getEntryId, entry.getId());
        ApReadBehavior behavior = getOne(wrapper);
        if (behavior!=null) {
            //存在，则更新
            behavior.setCount(behavior.getCount() + dto.getCount());
            behavior.setReadDuration(behavior.getReadDuration() + dto.getReadDuration());
            behavior.setPercentage(dto.getPercentage());
            behavior.setLoadDuration(dto.getLoadDuration());
            behavior.setUpdatedTime(new Date());
        }else {
            //不存在，新增
            behavior = new ApReadBehavior();
            behavior.setEntryId(entry.getId());
            behavior.setCount(dto.getCount());
            behavior.setReadDuration(dto.getReadDuration());
            behavior.setUpdatedTime(new Date());
            behavior.setLoadDuration(dto.getLoadDuration());
            behavior.setPercentage(dto.getPercentage());
            behavior.setCreatedTime(new Date());
            behavior.setArticleId(Long.parseLong(dto.getArticleId()));
        }
        saveOrUpdate(behavior);
        //发送阅读消息到kafka
        UpdateArticleMessage message = new UpdateArticleMessage();
        message.setType(0);
        message.setArticleId(behavior.getArticleId());
        message.setAdd(1);
        String jsonString = JSON.toJSONString(message);
        kafkaTemplate.send(hotArticleScoreTopic,jsonString);
        //返回响应
        return ResponseResult.okResult();
    }
}
